//
//  XYBookListController.swift
//  XYReadBook
//
//  Created by fox on 2017/9/9.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit

/// 目录控制器
class XYBookListController: XYViewController {
    
    let navigationView = UIView()
    let closeBtn = UIButton()
    let titleLab = UILabel()
    
    
    let Identifier = "BookListIdentifier"
    var dataArr:[Chapters] = []
    private lazy var tableView:UITableView = {
        let tableView = UITableView.init(frame: CGRect.zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.register(XYBookListCell.self, forCellReuseIdentifier:self.Identifier)
        return tableView
    }()
    
    var bookName:String
    var bookID:String
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        loadData()
    }
    
    
    func setupUI() {
        navigationView.backgroundColor = UIColor.hm_color(withRed: 108, green: 204, blue: 248)
        titleLab.text = bookName
        titleLab.textAlignment = .center
        navigationView.addSubview(titleLab)
        navigationView.addSubview(closeBtn)
        closeBtn.setTitle("返回", for: .normal)
        closeBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
        view.addSubview(tableView)
    }
    
    
    func close() {
        navigationController?.popViewController(animated: true)
    }
    
    
    func loadData() {
        // http://api.zhuishushenqi.com/mix-atoc/51d11e782de6405c45000068
        let urlKey = String(format: "mix-atoc/%@", self.bookID)
        let url =  XYAPI.baseurlFormat(baseURL: BaseUrl.defult.rawValue, key: urlKey)
        
        HTTPManager.requestModel(url: url, method: .get, parameters: nil, iscaChe: true) { (resp:XYChapterListModel?, error) in
            if let dataArr = resp?.mixToc?.chapters {
                self.dataArr = dataArr
                self.tableView.reloadData()
            }
        }
    }
    
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tableView.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
    }
    
    
    init(bookID:String,bookName:String) {
        self.bookID = bookID
        self.bookName = bookName
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



extension XYBookListController:UITableViewDataSource,UITableViewDelegate{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: self.Identifier, for: indexPath)as!XYBookListCell
        cell.model = dataArr[indexPath.row]
        
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if dataArr.count == 0 {
            return
        }
    }
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let headerView = XYBooklistHeaderView.init(frame: CGRect.init(x: 0, y: 0, width: ScreenWidth, height: 44), chapterCount: dataArr.count + 1)
        headerView.delegate = self
        return headerView
    }
}


extension XYBookListController:XYBooklistHeaderViewDelegate {

    func bookChapterlistSort(bookListView: XYBooklistHeaderView) {
        XYLog(messsage: "点击了排序")
    }
    
    func booklistRefresh(bookListView: XYBooklistHeaderView) {
        XYLog(messsage: "点击了刷新")
    }
}
